/*

	Paper tables
		Makes the tables for the "Remedial Education: Evidence from a Sequence of Experiments in Colombia" paper

*/

*Set directory here at the top
global dir "C:\Users\juuli\Dropbox\PaperManizales\7. Text\Publication\Harvard Dataverse"

run "$dir/insertrow.ado"
run "$dir/swaprows.ado"
run "$dir/quantile_treatment.ado"
/*
capture ssc install texsave
capture ssc install outreg2
capture ssc install ivreg2
capture ssc install ranktest
capture ssc install ivqte
capture ssc install moremata
capture ssc install kdens
*/

*********************************************************************************
//TABLE 1

/*In the published paper, for Table 1 we report non-standardized means and sd for each outcome. Publishing the non-standardized scores could lead to violations of confidentiality of students, teachers or schools. For that reason, we have substituted those variables with the standardized versions (except for math and literacy percent scores). If you have further requests you can contact the authors of the article. See aggregate statistics for not-standardized scores in file 'scorestabs.dta'.*/

  use "$dir/datawide.dta", clear
 
  bys schoolid cohort: gen n_school=_n
  
  global balance1 "age female reading_std_b sound_std_b words_std_b readcomp_std_b allliter_prc_b addition_std_b substract_std_b allmath_prc_b"
  local V1: word count $balance1
  local V1 = `V1'
  matrix P1 = J(`V1',3,.)
  forvalues c = 1(1)3{
    //means
    tabstat $balance1 if cohort==`c', by(T) format(%9.2f) notot save
    matrix R`c'1 = r(Stat2)',r(Stat1)'
    //pvalues
    local v = 1
    foreach var in $balance1{
	  ereturn clear
      capture areg `var' T if cohort==`c', a(allblock) cluster(schoolid)
	  if _rc==0 matrix P1[`v',`c'] =2*(1-normal(abs(_b[T]/_se[T])))
	  local ++v
    }
  }
  global balance2 "schoolsize morning ruralschool estrato_school avg_classsize nclass"
  local V2: word count $balance2
  local V2 = `V2'
  matrix P2 = J(`V2',3,.)
  forvalues c = 1(1)3{
    //means
    tabstat $balance2 if cohort==`c' & n_school==1, by(T) format(%9.2f) notot save
    matrix R`c'2 = r(Stat2)',r(Stat1)'
    //pvalues
    local v = 1
    foreach var in $balance2{
	  ereturn clear
      capture areg `var' T if cohort==`c' & n_school==1, a(allblock) cluster(schoolid)
	  if _rc==0 matrix P2[`v',`c'] =2*(1-normal(abs(_b[T]/_se[T])))
	  local ++v
    }
  } 
  
  global balance3 "tutorial_size tutorial_sd everattend attendance_prc attendance_days attrition_e attrition_m attrition_f"
  local V3: word count $balance3
  local V3 = `V3'
  matrix P3 = J(`V3',3,.)
  forvalues c = 1(1)3{
    //means
    tabstat $balance3 if cohort==`c', by(T) format(%9.2f) notot save
    matrix R`c'3 = r(Stat2)',r(Stat1)'
    //pvalues
    local v = 1
    foreach var in $balance3{
	  ereturn clear
      capture areg `var' T if cohort==`c', a(allblock) cluster(schoolid)
	  if _rc==0 matrix P3[`v',`c'] =2*(1-normal(abs(_b[T]/_se[T])))
	  local ++v
    }
  }
  
  //compilation  
  forvalues i=1(1)3{
	matrix T`i' = R1`i',P`i'[1..`V`i'',1],J(`V`i'',1,.), R2`i',P`i'[1..`V`i'',2],J(`V`i'',1,.),R3`i',P`i'[1..`V`i'',3]
  } 
  matrix T2= T1 \T2 \T3
  
  mat list T2
  drop _all
  svmat T2
  
  insertrow, place(before) row(1)
  insertrow, place(before) row(4)
  insertrow, place(before) row(13)
  insertrow, place(before) row(20)
  insertrow, place(before) row(23)
  insertrow, place(before) row(27)
 

  gen Panel = "" 
  replace Panel = "Panel A:" in 1
  replace Panel = "Panel B:" in 20
  replace Panel = "Panel C:" in 23
  replace Panel = "Panel D:" in 27
  
  gen Variables=""
  replace Variables="Avg. student's pre-treatment characteristics" in 1
  replace Variables="\hspace{2mm}  Age" if _n==2
  replace Variables="\hspace{2mm} Proportion female" if _n==3
  replace Variables="\hspace{2mm}  Scores:" in 4
  replace Variables="\hspace{2mm}  Fluency of oral reading" if _n==5
  replace Variables="\hspace{2mm} Knowledge of letter sounds" if _n==6
  replace Variables="\hspace{2mm} Reading of non-words" if _n==7
  replace Variables="\hspace{2mm} Reading comprehension" if _n==8
  replace Variables="\hspace{2mm} Literacy score (avg. of sub-scores)" if _n==9
  replace Variables="\hspace{2mm}  Additions" if _n==10
  replace Variables="\hspace{2mm} Subtractions" if _n==11
  replace Variables="\hspace{2mm} Math score (avg. of sub-scores)" if _n==12
  replace Variables="Avg. school's pre-treatment characteristics" if _n==13 
  replace Variables="\hspace{2mm} Grade 3 enrollment" in 14
  replace Variables="\hspace{2mm} Morning" in 15
  replace Variables="\hspace{2mm} Rural" in 16
  replace Variables="\hspace{2mm} Avg. Socio-ec. status (1-6)" in 17  
  replace Variables="\hspace{2mm} Avg. class size" in 18
  replace Variables="\hspace{2mm} Avg. number of classrooms in Grade 3" in 19  
  replace Variables="Tutorial assignment" in 20
  replace Variables="\hspace{2mm} Avg. tutorial size" if _n==21
  replace Variables="\hspace{2mm} S.D. literacy score of students in tutorial" if _n==22
  replace Variables="Compliance" in 23
  replace Variables="\hspace{2mm} Ever attended a tutorial" if _n==24
  replace Variables="\hspace{2mm} Tutorial attendance (percent)" if _n==25
  replace Variables="\hspace{2mm} Tutorial attendance (sessions)" if _n==26
  replace Variables="Attrition" in 27
  replace Variables="\hspace{2mm} End of Grade 3" in 28
  replace Variables="\hspace{2mm} Beg./Mid. Grade 4" in 29
  replace Variables="\hspace{2mm} End Grade 4" in 30
  order Panel Variables , b(T21)
  forvalues j = 1(1)11{
    gen aux = string(T2`j', "%9.2f")
	replace aux = "" if aux=="."
	drop T2`j'
	ren aux T2`j'
  }
  
  label var T21 "T"
  label var T22 "C"
  label var T23 "p-value"
  label var T25 "T"
  label var T26 "C"
  label var T27 "p-value"
  label var T29 "T"
  label var T210 "C"
  label var T211 "p-value"
  
  
    foreach var of varlist T22 T26 T210{
	replace `var'="0.00" if `var'=="-0.00" 
 }

  #delimit ;
  global note1 
  "Note: Panel A shows the average pre-treatment characteristics of eligible students. 
  Scores for subtasks are expressed in number of correct responses (fluency of oral reading, 
  reading of non-words, reading comprehension, additions, subtractions). 
  Literacy and math scores are expressed as average proportion of correct answers in each subtask.
  Panel B shows the attrition rates at different time horizons of the eligible students observed at baseline. 
  Panel C shows which eligible students attended the tutorials and the intensity of attendance. 
  Columns labeled 'T' show the average for students in schools randomized to treatment. 
  Columns labeled 'C' show the average for students in schools randomized to control. 
  Columns labeled 'p-val' show the p-value of a test of \(H_0:\theta=0\) (see equation (1)). 
  We present these statistics by cohort.";
  #delimit cr
  texsave using "$dir/results/Table_Balance.tex", width(1.2\textwidth) frag ///
  headerlines(" & \multicolumn{3}{c}{Cohort 1} & & \multicolumn{3}{c}{Cohort 2} & & \multicolumn{3}{c}{Cohort 3}") ///
  replace size(scriptsize) align(llCCCp{0.001cm}CCCp{0.001cm}CCC) location(ht) varlabels rowsep(1ex) ///
  title(Balance, Attrition, and Compliance) marker(tab:balance) ///
  footnote("$note1", size(scriptsize)) nofix
  

  

//TABLE 2
/*For not-standardized control baseline means and SD we use already computed aggregate statistics available in file 'scorestabs.dta'. We are doing this because publishing the microdata on non-standardized scores could lead to violations of confidentiality of students, teachers or schools. */

  use  "$dir/datalong.dta", clear
  global outregoptions = "keep(T_date_2 T_date_3 T_date_4) dec(3) label bracket excel nocons nor2 noobs nonotes dta slow(2000)"
  global mainoutcomes = "sound words reading readcomp allliter" 
  local j = 1
  foreach Y in $mainoutcomes{
   //replace option for outreg
   if `j'==1 local rep = "replace"
   if `j'>1  local rep = ""

   //estimation
   areg `Y'_std_ T_date_2 T_date_3 T_date_4 i.date i.year if date>0, a(allblock) cluster(schoolid)
   local N = e(N)
   test T_date_2=T_date_3=T_date_4
   local p = r(p)
   local p = string(`p',"%9.3f")
   outreg2 using "$dir/results/T2.xml", `rep'  $outregoptions addstat("Observations", `N') addtext("p-value of equal coeffs.", `p')
   local j = `j'+1
  }
 erase "$dir/results/T2.txt"
 use "$dir/results/T2_dta.dta",clear
 swaprows, swap(1) and(2)
 replace v1 = "" in 1
 erase "$dir/results/T2_dta.dta"
 erase "$dir/results/T2.xml"
 
 drop if _n==1
 label var v2 "Knowledge of letter sounds"
 label var v3 "Reading of non-words"
 label var v4 "Fluency of oral reading"
 label var v5 "Reading comprehension"
 label var v6 "Literacy score"
 
 //means and sd for control at baseline
 preserve
 use "$dir/scorestabs.dta", clear
 keep if date==0 & T==0
 keep date T sound_rc_mean words_rc_mean reading_rc_mean readcomp_rc_mean allliter_prc_mean sound_rc_sd words_rc_sd reading_rc_sd readcomp_rc_sd allliter_prc_sd
 reshape long sound_rc_ words_rc_ reading_rc_ readcomp_rc_ allliter_prc_, i(date T) j(v1) string
 drop date T
 rename (sound_rc_ words_rc_ reading_rc_ readcomp_rc_ allliter_prc_) (v2 v3 v4 v5 v6)
 forvalues i=2(1)6{
 tostring v`i', format(%9.3f) replace force
	
 }
 tempfile stats
 save `stats', replace
 restore
 
 append using `stats'
 replace v1="Baseline control mean (not std.)" if v1=="mean"
 replace v1="Baseline control S.D. (not std.)" if v1=="sd"
 
 #delimit ;
 global note1 "Note: Each column shows the coefficients \(\theta_{h}\) of equation (2), that is, the estimated treatment effects at different time horizons for each outcome of interest. The row labeled 'p-value of equal coeffs' shows the p-value of a test \(H_0:\theta_{1}=\theta_{2}=\theta_{3}\). Control mean and S.D. correspond to the \# of correct answers (cols. 1-4) or the \% of correct answers (col. 5). All models include cohort, year, and strata fixed effects. Standard errors, shown in squared brackets, are clustered at the school-level (the unit of randomization). * significant at 10\%; ** significant at 5\%; *** significant at 1\%.";
 #delimit cr
 texsave using "$dir/results/Table_MainOuts.tex", width(\textwidth) ///
 replace size(scriptsize) align(lCCCCC) location(ht) varlabels frag ///
 title(Treatment Effects on Literacy \\ (standardized literacy outcomes)) marker(tab:mainouts) ///
 footnote("$note1", size(scriptsize)) nofix

 
// TABLE 4: EFFECT ON OTHER OUTCOMES
/*For not-standardized control baseline means and SD we use already computed aggregate statistics available in file 'scorestabs.dta'. We are doing this  because publishing the microdata of non-standardized scores could lead to violations of confidentiality of students, teachers or schools. */

  use "$dir/datalong.dta", clear
  global outregoptions = " dec(3) label bracket excel nocons nor2 noobs nonotes dta slow(2000)"
  global otheroutcomes = "addition substract allmath repeat" 
  local j = 1
  foreach Y in $otheroutcomes{
   //replace option for outreg
   if `j'==1 local rep = "replace"
   if `j'>1  local rep = ""
   //estimation
   if "`Y'"=="addition" | "`Y'"=="substract" | "`Y'"=="allmath"{
    areg `Y'_std_ T_date_2 T_date_3 T_date_4 i.date i.year if date>0, a(allblock) cluster(schoolid)
    local N = e(N)
    test T_date_2=T_date_3=T_date_4
    local p = r(p)
	local p = string(`p',"%9.3f")
    outreg2 using "$dir/results/T2.xml", `rep'  keep(T_date_2 T_date_3 T_date_4) $outregoptions addstat("Observations", `N') addtext("p-value of equal coeffs.", `p')
   }
   if "`Y'"=="repeat" {
    areg `Y' T_date_2  if date==1, a(allblock) cluster(schoolid)
    local N = e(N)
    outreg2 using "$dir/results/T2.xml", `rep'  keep(T_date_2) $outregoptions addstat("Observations", `N')
   }

   local j = `j'+1
  }

 erase "$dir/results/T2.txt"
 use "$dir/results/T2_dta.dta",clear
 swaprows, swap(1) and(2)
 replace v1 = "" in 1
 erase "$dir/results/T2_dta.dta"
 
 label var v2 "Additions (std.)"
 label var v3 "Subtractions (std.)"
 label var v4 "Math score (std.)"
 label var v5 "Repeat Grade 3"

 drop if _n==1
 
 //means and sd for control at baseline
 preserve
 use "$dir/scorestabs.dta", clear
 keep if date==0 & T==0
 keep date T addition_rc_mean substract_rc_mean allmath_prc_mean repeat_mean addition_rc_sd substract_rc_sd allmath_prc_sd repeat_sd
 reshape long addition_rc_ substract_rc_ allmath_prc_ repeat_, i(date T) j(v1) string
 drop date T
 rename (addition_rc_ substract_rc_ allmath_prc_ repeat_) (v2 v3 v4 v5)
 forvalues i=2(1)5{
 tostring v`i', format(%9.3f) replace force
	
 }
 tempfile stats
 save `stats', replace
 restore
 
 append using `stats'
 replace v1="Baseline control mean (not std.)" if v1=="mean"
 replace v1="Baseline control S.D. (not std.)" if v1=="sd"
 
 #delimit ;
 global note1 "Note: Each column shows the coefficients \(\theta_{h}\) of equation (2), that is, the estimated treatment effects at different time horizons for each outcome of interest. The row labeled 'p-value of equal coeffs' shows the p-value of a test \(H_0:\theta_{1}=\theta_{2}=\theta_{3}\). Control mean and S.D. correspond to the baseline \# of correct answers (cols. 1-3) or the proportion of students who repeated at the end of grade 3 (col. 4). All models include cohort, year, and strata fixed effects. Standard errors, shown in squared brackets, are clustered at the school-level (the unit of randomization). * significant at 10\%; ** significant at 5\%; *** significant at 1\%.";
 #delimit cr
 
 texsave using "$dir/results/Table_OtherOuts.tex", width(0.8\textwidth) ///
 replace size(scriptsize) align(lCCCCC) location(ht) varlabels frag ///
 title(Treatment Effects on other Education Outcomes) marker(tab:otherouts) ///
 footnote("$note1", size(scriptsize)) nofix

 
//TABLE 5
/*For not-standardized control baseline means and SD we use already computed aggregate statistics available in file 'scorestabs.dta'. We are doing this  because publishing the microdata of non-standardized scores could lead to violations of confidentiality of students, teachers or schools. */

 use "$dir/datalong.dta", clear
 
 global outregoptions = " dec(3) label bracket excel nocons nor2 noobs nonotes dta slow(2000)"
 global mainoutcomes = "sound words reading readcomp allliter" 
 global otheroutcomes = "addition substract allmath" 

 local j = 1
 local F_stat = 1000000000
 foreach Y in $mainoutcomes{
  //replace option for outreg
  if `j'==1 local rep = "replace"
  if `j'>1  local rep = ""

  //IV
  ivreg2 `Y'_std_ (attendance_days_date_1 attendance_days_date_2 attendance_days_date_3 = T_date_1 T_date_2 T_date_3)  i.year i.allblock if date>0, cluster(schoolid) partial(i.year i.allblock)
  local  F_stat = min( e(rkf), `F_stat') 
  local N = e(N)
  test attendance_days_date_1=attendance_days_date_2=attendance_days_date_3
  local p = r(p)
  local p = string(`p',"%9.3f")
  di in red `p'
  outreg2 using "$dir/results/T3.xml", `rep'  keep(attendance_days_date_1 attendance_days_date_2 attendance_days_date_3) $outregoptions addstat("Observations", `N')  addtext("p-value of equal coeffs.", `p')
  local j = `j'+1
 }
 
  
 erase "$dir/results/T3.txt"
 use "$dir/results/T3_dta.dta",clear
 swaprows, swap(1) and(2)
 replace v1 = "" in 1
 replace v1 = "Days in tutorial x End of Grade 3" if v1=="Attendance days x (date 1)"
 replace v1 = "Days in tutorial x Beginning of Grade 4" if v1=="Attendance days x (date 2)"
 replace v1 = "Days in tutorial x End of Grade 4" if v1=="Attendance days x (date 3)"
 erase "$dir/results/T3_dta.dta"
 drop if _n==1
 label var v2 "Knowledge of letter sounds"
 label var v3 "Reading of non-words"
 label var v4 "Fluency of oral reading"
 label var v5 "Reading comprehension"
 label var v6 "Literacy score"
 
  
 //means and sd for control at baseline
 preserve
 use "$dir/scorestabs.dta", clear
 keep if date==0 & T==0
 keep date T sound_rc_mean words_rc_mean reading_rc_mean readcomp_rc_mean allliter_prc_mean sound_rc_sd words_rc_sd reading_rc_sd readcomp_rc_sd allliter_prc_sd
 reshape long sound_rc_ words_rc_ reading_rc_ readcomp_rc_ allliter_prc_, i(date T) j(v1) string
 drop date T
 rename (sound_rc_ words_rc_ reading_rc_ readcomp_rc_ allliter_prc_) (v2 v3 v4 v5 v6)
 forvalues i=2(1)6{
 tostring v`i', format(%9.3f) replace force
	
 }
 tempfile stats
 save `stats', replace
 restore
 
 append using `stats'
 replace v1="Baseline control mean (not std.)" if v1=="mean"
 replace v1="Baseline control S.D. (not std.)" if v1=="sd"
 
  #delimit ;
  global note1 "Note: Each column shows the estimates of the coefficients \(\beta_h\) of equation (3), that is, the estimated dose response at different time horizons for each outcome of interest. Dosage is measured by the number of days in which students attended the tutorial. The actual attendance was instrumented with the randomized treatment indicator variable. The first stage Kleibergen-Paap F-statistic is equal to 1828 and the associated p-value is 0. The row labeled 'p-value of equal coeffs.' shows the p-value of a test \(H_0:\beta_1=\beta_2=\beta_3\). Control mean and S.D. correspond to the \# of correct answers (cols. 1-4) or the \% of correct answers (col. 5). All models include cohort, year, and strata fixed effects. Standard errors, shown in squared brackets, are clustered at the school-level (the unit of randomization). * significant at 10\%; ** significant at 5\%; *** significant at 1\%.";
  #delimit cr
  texsave using "$dir/results/Table_doseresponse.tex", width(0.88\textwidth) ///
  replace size(scriptsize) align(lCCCCC) location(ht) varlabels frag ///
  title(Dose-response Effects \\ Instrumental Variables Estimates, standardized outcomes) marker(tab:doseresponse) ///
  footnote("$note1", size(scriptsize)) nofix
  erase "$dir/results/T3.xml"


//TABLE 6: Targeting
/*We are not able to publish files for replicating Table 6 since this would imply publishing the microdata on not-standardized scores that could lead to violations of confidentiality of students, teachers or schools. For any question regarding this table please contact Matias Busso at matias.busso.econ@gmail.com*/

 
 
// TABLE 7: TREATMENT EFFECT HETEROGENEITY
 use  "$dir/datalong.dta", clear
 
 global outregoptions = " dec(3) label bracket excel nocons nor2 noobs nonotes dta"
 global mainoutcomes = "sound words reading readcomp allliter" 
 local k = 1
 foreach g in small goodpeers homogroup experienced_tutor{
  if "`g'"!="experienced_tutor"{
   cap drop aux
   gen aux = 0 if T==0
   replace aux = 1 if `g'==1 & T==1
   replace aux = 2 if `g'==0 & T==1
   replace `g' = aux
  }
  if "`g'"=="experienced_tutor" replace experienced_tutor = . if cohort==1
  
  local j = 1
  foreach Y in $mainoutcomes{
   //replace option for outreg
   if `j'==1 local rep = "replace"
   if `j'>1  local rep = ""
   cap drop group
   cap drop T_group*
   gen group = `g'
   gen T_group = T_date_2 * group
   tab T_group, gen(T_group_)
   areg `Y'_std_ T_group_2 T_group_3 if date==1 , a(allblock) cluster(schoolid)  
   test T_group_2=T_group_3
   local p = r(p)
   local p = string(`p',"%9.3f")
   sleep 2000
   outreg2 using "$dir/results/T4_`k'.xml", `rep' keep(T_group_2 T_group_3) $outregoptions addtext("p-value of equal coeffs.", `p')
   local j = `j'+1
   }
  local k = `k'+1
}

 use "$dir/results/T4_1_dta.dta", clear
 forvalues k = 2(1)4{
   cap erase "$dir/results/T4_`k'.txt"
   append using "$dir/results/T4_`k'_dta.dta"
 }

 drop if _n==8 | _n==10 | _n==11 | _n==17 | _n==19 | _n==20 | _n==26 | _n==28 | _n==29 | _n==35 | _n==37 | _n==38 | _n==44 
 gen panel = ""
 order panel
 replace panel = "Size of tutorial" in 4
 replace panel = "group" in 5
 replace panel = "Peers' initial" in 10
 replace panel = "ability" in 11
 replace panel = "Homogeneity" in 16
 replace panel = "of tutorial group" in 17
 replace panel = "Tutor's previous" in 22
 replace panel = "experience" in 23
 
 replace v1 = "" in 2
 replace v1 = "1-5 students" in 4
 replace v1 = "6 students" in 6
 replace v1 = "High" in 10
 replace v1 = "Low" in 12
 replace v1 = "s.d. below median" in 16
 replace v1 = "s.d. above median" in 18
 replace v1 = "No" in 22
 replace v1 = "Yes" in 24

 swaprows, swap(1) and(2)
 forvalues j = 1(1)6{
   replace v`j'= regexr(v`j',"\(standardized\)","")
 }
 compress 
   drop if _n==1
  label var v2 "Knowledge of letter sounds"
  label var v3 "Reading of non-words"
  label var v4 "Fluency of oral reading"
  label var v5 "Reading comprehension"
  label var v6 "Literacy score"
 #delimit ;
 global notehet " Note: Each panel shows estimates of \(\theta_1\) of equation (2) 
 estimated separately for two different groups of treated students where the comparison is
 against all the students in the control group. The row 'p-value of equal coeffs.'
 shows the p-value of a Chow-test of equality of coefficients in the different samples. 
 See text for the description of each dimension of heterogeneity that is explored in the table. 
 All models include cohort, year, and strata fixed effects. Standard errors, shown in squared brackets, 
 are clustered at the school-level (the unit of randomization).  * significant at 10\%; ** 
 significant at 5\%; *** significant at 1\%.";
 #delimit cr
 texsave using "$dir/results/Table_Heterogeneity.tex", width(\textwidth) ///
 replace size(scriptsize) align(llCCCCC) location(ht) varlabels frag ///
 title(Treatment Effect Heterogeneity \\ (standardized outcomes)) marker(tab:heterogeneity) ///
 footnote("$notehet", size(scriptsize)) nofix
 
 forvalues j =1(1)5{
  cap erase "$dir/results/T4_`j'_dta.dta"
  cap erase "$dir/results/T4_`j'.xml"
  cap erase "$dir/results/T4_`j'_a_dta.dta"
  cap erase "$dir/results/T4_`j'_b_dta.dta"
  cap erase "$dir/results/T4_`j'_a.xml"
  cap erase "$dir/results/T4_`j'_b.xml"
  }
  

//FIGURE 1: Done in Ipe software

//FIGURE 2: For replication material on Figure 2 contact Matias Busso at matias.busso.econ@gmail.com

//FIGURE 3:
  use "$dir/scorestabs.dta", clear
  keep allliter_rc_mean date T
  ren allliter_rc_mean Y_allliter_rc
 
  reshape long Y_, i(date T) j(outcome) string
  reshape wide Y_ , i(date outcome) j(T)

  label var Y_0 "Control"
  label var Y_1 "Treated"
  #delimit ;
  twoway (connected Y_0 Y_1 date, mcolor(gray black) msymbol(circle circle) lpattern(dash solid) lcolor(gray black) ) , legend(order(1 "Control" 2 "Treated"))
          ytitle(Literacy score (# of correct answers)) yscale(range(80 150)) ylabel(, nogrid) xtitle(Date of test)   
		  xlabel(0 "Beg. G3" 1 "End G3" 2 "Beg./Mid. G4" 3 "End G4", labsize(small))
		  graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) 
		  plotregion(fcolor(white) ifcolor(white)) xline(0.5, lpattern(dot) lcolor(black)) text(141 0.75 "Intervention", size(small));
  #delimit cr
  graph export "$dir/results/Fig_MainResult.pdf", as(pdf) replace                                                                                                                
 
//FIGURE 4 AND APPENDIX TABLE A4
  use "$dir/datalong.dta", clear
  tempname memhold
  tempfile results
  postfile `memhold' str20 outcome date b1 b2 b3 se1 se2 se3 using `results'
  foreach outcome in sound words reading readcomp allliter{
    forvalues d =1(1)3{
     areg `outcome'_std_  T_cohort_2 T_cohort_3 T_cohort_4  if date==`d', a(allblock) cluster(schoolid)
     post `memhold' ("`outcome'") (`d') (_b[T_cohort_2]) (_b[T_cohort_3]) (_b[T_cohort_4]) (_se[T_cohort_2]) (_se[T_cohort_3]) (_se[T_cohort_4])
    }
  }
  postclose `memhold'
  use `results', clear
  reshape long b se, i(outcome date) j(c)
  gen cohort = "Cohort 1" if c==1
  replace cohort = "Cohort 2" if c==2
  replace cohort = "Cohort 3" if c==3
  replace b = . if b==0
  replace se = . if se==0
  label var b "Estimated treatment effects"
  label var cohort "Experimental cohort"
  gen lb = b - invnorm(0.975)*se
  gen ub = b + invnorm(0.975)*se
 
  #delimit ;
  twoway (bar b c if outcome=="allliter" & date==1, 
          fcolor(black%40) lcolor(black%40) barwidth(0.5)) 
		 (rcap ub lb c if outcome=="allliter" & date==1, 
		 lcolor(black) lwidth(medthick)) 
		 (scatter b c, mcolor(gray%40) msymbol(circle)), 
		 ytitle(Estimated treatment effects) 
		 ylabel(0(0.2)0.8)		 
		 ytitle(, size(small)) 
		 ylabel(, labsize(small) glcolor(white)) 
		 xtitle(Cohort) 
		 xtitle(, size(small)) 
		 xlabel(#3, labsize(small) glcolor(white)) 
		 legend(off) graphregion(fcolor(white) 
		 lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white));
  #delimit cr
  graph export "$dir/results/Fig_TE_by_cohort.pdf", as(pdf) replace

  // APPENDIX TABLE A4
  gen order = 1 if outcome=="sound"
  replace order = 2 if outcome=="words"
  replace order = 3 if outcome=="reading"
  replace order = 4 if outcome=="readcomp"
  replace order = 5 if outcome=="allliter"
  gen th = "End of Grade 3" if date==1
  replace th = "Beg/Med. Grade 4" if date==2
  replace th = "End of Grade 4" if date==3
  replace outcome = "Knowledge of letter sounds" if outcome=="sound"
  replace outcome = "Reading of non-words" if outcome=="words"
  replace outcome = "Fluency of oral reading" if outcome=="reading"
  replace outcome = "Reading comprehension" if outcome=="readcomp"
  replace outcome = "Literacy score" if outcome=="allliter"
  order cohort th outcome
  sort c date order
  replace cohort = "" if order!=1
  replace cohort = "" if regex(th,"4")==1
  replace th = "" if order!=1
  drop c date order
  gen p = 2*(normal(-abs(b/se)))
  gen stars = "*" if p<=0.1
  replace stars = "**" if p<=0.05
  replace stars = "***" if p<=0.01
  gen TE = string(b,"%9.3f")
  gen SE = "[" + string(se,"%9.3f") + "]" + stars
  drop stars p b se
  replace TE = "-" if TE=="."
  replace SE = "-" if SE=="[.]"
  
  label var th "Moment in time"
  label var outcome "Outcome"
  label var TE "Treatment effect"
  label var SE "Standard error"
  cap drop ub lb
  #delimit ;
  global noteapp1 "Note: Each panel shows separate estimates of the treatment effects for each cohort.
  We estimate the models separately for each time-horizon and for each outcome of interest. 
  All models include strata fixed effects. We did not collected data on the outcomes without an estimated 
  treatment effect (marked as -). Standard errors, shown in squared brackets, are clustered at the school-level 
  (the unit of randomization). * significant at 10\%; ** significant at 5\%; *** significant at 1\%.";
  #delimit cr
  texsave using "$dir/results/Appendix_Table_allresults.tex", width(0.88\textwidth) ///
  replace size(scriptsize) align(lllCC) location(htbp) varlabels frag ///
  title(Treatment Effects on all outcomes and all time horizons) marker(tab:app_table_allresults) ///
  footnote("$noteapp1", size(scriptsize)) nofix

  
 
//FIGURE 5
  use "$dir/datalong.dta", clear
  collapse (mean) attendance_days T, by(schoolid cohort)
  keep if T==1
  sort cohort attendance_
  by cohort: gen id = _n
  drop school T
  ren attendance_ A
  reshape wide A, i(id) j(cohort)
  twoway (line A1 id, lcolor(black%70) lwidth(thick) lpattern(longdash) yline(36, lwidth(medthick) lpattern(dot) lcolor(black)))   /*
    */ (line A2 id, lcolor(gray%70) lwidth(thick) lpattern(solid) yline(48, lwidth(medthick) lpattern(dot) lcolor(black))) /*
	*/ (line A3 id, lcolor(black%50) lwidth(thick) lpattern(dash)), /*
	*/  ytitle(Attendance (days)) ylabel(, nogrid) /*
	*/  xtitle(Treated schools (sorted by attendance)) /*
	*/  xtitle(, size(small) margin(small)) xlabel(#25, labsize(vsmall) noticks) /*
	*/  legend(order(1 "Cohort 1" 2 "Cohort 2" 3 "Cohort 3") rows(1) size(medsmall) /*
	*/  region(fcolor(white) lcolor(white))) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) /*
	*/  plotregion(fcolor(white) ifcolor(white) ilcolor(white))
  graph export "$dir/results/Fig_compliance.pdf", as(pdf) replace

  
///FIGURE 6
 use "$dir/datalong.dta", clear
 gen one = 1
 //estimation
 quantileTE sound_std_, treatment(T) fixedeffects(i.cohort) controls(one) strata(allblock)  save("$dir/results/sound.dta")
 quantileTE reading_std_ if cohort>1, treatment(T) fixedeffects(i.cohort) controls(one) strata(allblock) save("$dir/results/reading.dta")
 quantileTE words_std_, treatment(T) fixedeffects(i.cohort) controls(one) strata(allblock) save("$dir/results/words.dta")
 quantileTE allliter_std_, treatment(T) fixedeffects(i.cohort) controls(one) strata(allblock) save("$dir/results/allliter.dta")
 quantileTE readcomp_std_, treatment(T) fixedeffects(i.cohort) controls(readcomp_baseline) strata(allblock) save("$dir/results/readcomp.dta")

 //compile
 use "$dir/results/sound.dta", clear
 foreach var in reading words readcomp allliter{
   append using "$dir/results/`var'.dta"
 }
 //plot
 foreach var in sound reading words readcomp allliter{
  quantplot, est(firpo) name(`var')
  }
  

 // combine all figures
  cd "$dir/results/"
  #delimit ;
  graph combine "plot_firpo_sound.gph"   "plot_firpo_words.gph" 
				"plot_firpo_reading.gph" "plot_firpo_readcomp.gph" 
				"plot_firpo_allliter.gph" 
				,
				graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) 
				plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white));
  graph export "$dir/results/Fig_firpo.pdf", as(pdf) replace;

  #delimit cr

  // clean up
  foreach var in sound reading words readcomp allliter{
    erase "$dir/results/plot_firpo_`var'.gph"
	erase "$dir/results/`var'.dta"
  }



//APPENDIX TABLE A1
  use "$dir/datalong.dta", clear

    
  global covariates = "age age2 female nodisab missdisab i.estrato class_size Nclasses rural morning"
  global outregoptions = "keep(T_date_2 T_date_3 T_date_4) dec(3) label bracket excel nocons nor2 noobs nonotes dta slow(2000)"
  global mainoutcomes = "sound words reading readcomp allliter " 
  local j = 1
  foreach Y in $mainoutcomes{
   cap drop Y0
   gen Y0 = `Y'_baseline
   label var Y0 "Outcome at baseline (standardized)"
   //replace option for outreg
   if `j'==1 local rep = "replace"
   if `j'>1  local rep = ""
   //estimation (control baseline score)
   areg `Y'_std_ T_date_2 T_date_3 T_date_4 Y0 i.date i.year if date>0, a(allblock) cluster(schoolid)
   sleep 1000
   outreg2 using "$dir/results/T1.xml", `rep' $outregoptions
   //estimation (individual controls)
   areg `Y'_std_ T_date_2 T_date_3 T_date_4 $covariates i.year i.date if date>0, a(allblock) cluster(schoolid)
   sleep 1000
   outreg2 using "$dir/results/T2.xml", `rep'  $outregoptions
   //estimation (with school fixed effects)
   areg `Y'_std_ T_date_2 T_date_3 T_date_4 i.cohort i.date i.year if date>0, a(schoolid) cluster(schoolid)
   sleep 1000
   outreg2 using "$dir/results/T4.xml", `rep'  $outregoptions
   local j = `j'+1
  }

 erase "$dir/results/T1.txt"
 erase "$dir/results/T2.txt"
 erase "$dir/results/T4.txt"

 use "$dir/results/T1_dta.dta",clear
 gen model = "Control: Baseline outcome (Y0)"
 append using "$dir/results/T2_dta.dta"
 replace model = "Controls: Individual and School" if model==""
 append using "$dir/results/T4_dta.dta"
 replace model = "School fixed effects" if model==""
 swaprows, swap(1) and(2)
 replace v1 = "" in 1
 foreach i in 1 2 4{
  erase "$dir/results/T`i'_dta.dta"
  erase "$dir/results/T`i'.xml"
 }

 drop if _n==11 | _n==12 | _n==13 | _n==21 | _n==22 | _n==23
 label var v2 "Knowledge of letter sounds"
 label var v3 "Reading of non-words"
 label var v4 "Fluency of oral reading"
 label var v5 "Reading comprehension"
 label var v6 "Literacy score"

 compress
 order model
 replace model = "" if _n!=4 & _n!=11 & _n!=18
 #delimit ;
 global noterob "Note: Models in panel 1 include baseline outcome as control, cohort, year, and strata fixed effects. Models in panel 2 include as controls: age, age squared, gender, socio-economic and disability status, class size, school number of classes in grade 3, rural and morning schhol-status, along with cohort, year, and strata fixed effects. Models in panel 3 include year, cohort and school fixed effects instead of strata fixed effects. Each column-panel show the coefficients \(\theta_h\) of equation (2). That is, the estimated treatment effects at different time horizons for each outcome of interest. Standard errors, shown in squared brackets, are clustered at the school-level (the unit of randomization). * significant at 10\%; ** significant at 5\%; *** significant at 1\%.";
 #delimit cr
 drop if _n==1
 texsave using "$dir/results/Appendix_Table_Robust.tex", width(1.3\textwidth) ///
 replace size(scriptsize) align(llCCCCCCCC) location(p) varlabels frag ///
 title(Treatment Effects Robustness) marker(tab:robust) ///
 footnote("$noterob", size(scriptsize)) nofix
 
  /*WE EXPORT PART FOR TABLE 3*/
  drop model
  keep if _n==3 | _n==4 | _n==10 | _n==11 | _n==17 | _n==18
  save "$dir/results/rob_spec_dta.dta", replace
  

 
// APPENDIX TABLE A2: 
  use "$dir/datalong.dta", clear

  global outregoptions = "dec(3) label bracket excel nocons noobs nor2 nonotes dta slow(2000)"   
  global mainoutcomes = "sound words reading readcomp allliter" 
  gen selected=(reading_std_!=.)
  gen selected_sound=(sound_std_!=.)
  replace selected_sound=. if cohort==1 & date>1
  mat define A=J(15,6,.)
  local j=1
  foreach i in 1 2 3{ 
  foreach Y in $mainoutcomes{
		if "`Y'"=="sound" local selected_var = "selected_sound"
		if "`Y'"!="sound" local selected_var = "selected"
		leebounds `Y'_std_ T if date==`i', tight(cohort) select("`selected_var'")

		mat A[`j',1]=r(table)[1,1]
		mat A[`j',2]=r(table)[2,1]
		mat A[`j',3]=r(table)[4,1]
		mat A[`j',4]=r(table)[1,2]
		mat A[`j',5]=r(table)[2,2]
		mat A[`j',6]=r(table)[4,2]
	
		local j=`j'+1
  }
  }
  /*WE EXPORT PART FOR TABLE 3*/
  global outregoptions = "dec(3) label bracket excel nocons noobs nor2 nonotes dta slow(2000)" 
  local j=1
  foreach i in 1{ 
  foreach Y in $mainoutcomes{
   if `j'==1 local rep = "replace"
   if `j'>1  local rep = ""  
   if "`Y'"=="sound" local selected_var = "selected_sound"
   if "`Y'"!="sound" local selected_var = "selected"
   leebounds `Y'_std_ T if date==`i', tight(cohort) select("`selected_var'")
   outreg2 using "$dir/results/rob_lee.xml", `rep'  $outregoptions
 
   local j=`j'+1
  }
  } 
  
  
  drop _all
  svmat A
  rename (A3 A6) (pl pu)
  foreach var of varlist pl pu{
	gen stars_`var' = "*" if `var'<=0.1
	replace stars_`var' = "**" if `var'<=0.05
	replace stars_`var' = "***" if `var'<=0.01
  }
  foreach i in 1 4 {
		gen a`i'=string(A`i',"%9.3f") 
		drop A`i'
  }
  gen a2 = "[" + string(A2,"%9.3f") + "]" + stars_pl
  drop A2
  gen a5 = "[" + string(A5,"%9.3f") + "]" + stars_pu
  drop A5  
  drop stars_* p*
  order a*, alpha
  

  gen th = "End of Grade 3" if _n==1
  replace th = "Beg/Med. Grade 4" if _n==6
  replace th = "End of Grade 4" if _n==11
  gen outcome=""
  replace outcome = "Knowledge of letter sounds" if _n==1 | _n==6 | _n==11
  replace outcome = "Reading of non-words" if _n==2 | _n==7 | _n==12
  replace outcome = "Fluency of oral reading" if _n==3 | _n==8 | _n==13
  replace outcome = "Reading comprehension" if _n==4 | _n==9 | _n==14
  replace outcome = "Literacy score" if _n==5 | _n==10 | _n==15
  order th outcome

  insertrow, place(after) row(10)
  insertrow, place(after) row(5)

  label var outcome "Outcome"
  label var a1 "Lower Bound"
  label var a2 "SE LB"
  label var a4 "Upper Bound"
  label var a5 "SE UB"
  
  
  #delimit ;
  global noteapp1 "Note: Each panel shows separate estimates and standard errors of Lee (2009) bounds for each outcome. We estimate the models separately for each time-horizon and for each outcome of interest tightening at the cohort level (strata tightening is not possible since there is not enough attrition variation at this level). * significant at 10\%; ** significant at 5\%; *** significant at 1\%.";
  #delimit cr
  texsave using "$dir/results/Appendix_Table_leebounds.tex", width(\textwidth) ///
  replace size(scriptsize) align(llClCl) location(htbp) varlabels frag ///
  title(Robustness: Lee (2009) bounds) marker(tab:leebounds) ///
  footnote("$noteapp1", size(scriptsize)) nofix
  
  /*WE EXPORT PART FOR TABLE 3*/
  use "$dir/results/rob_lee_dta.dta", clear
  drop in 1/3
  save "$dir/results/rob_lee_dta.dta", replace
 
  cap erase "$dir/results/rob_lee.txt"
  cap erase "$dir/results/rob_lee.xml"

// APPENDIX TABLE A3:
 use "$dir/datalong.dta", clear
    
  wyoung sound_std words_std reading_std readcomp_std allliter_std, cmd(areg OUTCOMEVAR T_date_2 T_date_3 T_date_4 i.date i.year if date>0, a(allblock) cluster(schoolid)) familyp(T_date_2 T_date_3 T_date_4) bootstraps(1000) seed(123) cluster(schoolid)
  mat def A=r(table)
  local ri=1
  local rf=5
  forvalues i=1(1)3{
	  matselrc A B`i' , r(`ri'/`rf') c(1, 3/4)
	  mat def B`i'=B`i''
	  local ri=`ri'+5
	  local rf=`rf'+5
	  
  }
  mat V=B1\ B2\ B3
  drop _all
  svmat V
  forvalues j = 1(1)5{
	gen v`j' = string(V`j', "%9.3f")
	drop V`j' 
  }  
  
  gen v0=""
  replace v0="Treatment x End of Grade 3" if _n==1
  replace v0="Treatment x Beginning of Grade 4" if _n==4
  replace v0="Treatment x End of Grade 4" if _n==7
  foreach i in 2 5 8{
	 replace v0="\hspace{2mm p-value}" if _n==`i'
  }
  foreach i in 3 6 9{
	 replace v0="\hspace{2mm Adjusted p-value}" if _n==`i'
  } 
 
  insertrow, place(after) row(9)
  replace v0="Observations" in  10
  replace v1="4949" in 10
  forvalues i=2(1)5{
	 replace v`i'="6362" in 10
  }
  insertrow, place(after) row(9)
  insertrow, place(after) row(6)
  insertrow, place(after) row(3)

  label var v1 "Knowledge of letter sounds"
  label var v2 "Reading of non-words"
  label var v3 "Fluency of oral reading"
  label var v4 "Reading comprehension"
  label var v5 "Literacy score"
   
  order v*, alpha
  
  insertrow, place(before) row(1) 
  local i=1
  foreach var of varlist v1-v5{
	  replace `var'="(`i')" in 1
     local i=`i'+1
  }
  insertrow, place(after) row(1) 
  
  local h=6
  forvalues i=5(-1)0{
	 rename v`i' v`h'
     local h=`h'-1
  }
    
  
  #delimit ;
  global note1 "Note: Each column shows the coefficients \(\theta_{h}\) of equation (2), that is, the estimated treatment effects at different time horizons for each outcome of interest. Point estimates and p-values are those of Table \ref{tab:mainouts}. Adjusted p-values are calculated using Westfall-Young stepdown methodology. All models include cohort, year, and strata fixed effects. Standard errors, shown in squared brackets, are clustered at the school-level (the unit of randomization). * significant at 10\%; ** significant at 5\%; *** significant at 1\%.";
 #delimit cr
 texsave using "$dir/results/Appendix_Table_MHT.tex", width(\textwidth) ///
 replace size(scriptsize) align(lCCCCC) location(ht) varlabels frag ///
 title(Robustness: Multiple Hypothesis Testing) marker(tab:mht) ///
 footnote("$note1", size(scriptsize)) nofix
 
 /*WE EXPORT PART FOR TABLE 3**/
 keep if _n==3 | _n==5
 save  "$dir/results/rob_mht_dta.dta", replace
 
 
 
//TABLE 3
 use "$dir/results/rob_spec_dta.dta", clear
 append using  "$dir/results/rob_lee_dta.dta"
 append using  "$dir/results/rob_mht_dta.dta"

 insertrow, place(before) row(1) 
 insertrow, place(before) row(8) 

 replace v1="Panel A: Adding controls" if _n==1
 replace v1="Panel B: Lee-bounds" if _n==8
 replace v1="Panel C: Multiple Hypothesis Testing" if _n==13
 
 replace v1="\hspace{2mm Baseline outcome}" if _n==2
 replace v1="\hspace{2mm Individual- and school-level variables}" if _n==4
 replace v1="\hspace{2mm School fixed effects}" if _n==6
 
 replace v1="\hspace{2mm Lower bound}" if _n==9
 replace v1="\hspace{2mm Upper bound}" if _n==11

 replace v1="\hspace{2mm Treatment}" if _n==14

 insertrow, place(before) row(8) 
 insertrow, place(before) row(14) 
 
 insertrow, place(before) row(1) 
 local i=1
 foreach var of varlist v2-v6{
	replace `var'="(`i')" in 1
    local i=`i'+1
 }
 insertrow, place(after) row(1)   
     
 #delimit ;
 global note "Note: Table compiles different robustness checks for estimated treatment effects at the end of Grade 3. Panel A adds controls for the main specification. Individual-level controls include age, age squared, gender, socio-economic and disability status. School-level controls include class size, school number of classes in grade 3, rural and morning school-status. Panel B estimates Lee (2009) bounds. Panel C calculates Westfall-Young adjusted p-values to address for multiple hypothesis testing. Appendix Tables \ref{tab:robust}, \ref{tab:leebounds} and \ref{tab:mht} present details and the same robustness for beginning and end of Grade 4.  * significant at 10\%; ** significant at 5\%; *** significant at 1\%.";
 #delimit cr
 texsave using "$dir/results/Table_RobustnessAll.tex", width(\textwidth) ///
 replace size(scriptsize) align(lCCCCC) location(p) varlabels frag ///
 title(Robustness \\ Treatment Effects at the End of Grade 3 (standardized outcomes)) marker(tab:mainrobust) ///
 footnote("$note", size(scriptsize)) nofix  
  
  
 erase "$dir/results/rob_spec_dta.dta"
 erase "$dir/results/rob_lee_dta.dta"
 erase "$dir/results/rob_mht_dta.dta"  
  
  

  
// APPENDIX TABLE A5
 use "$dir/datalong.dta", clear
 
 global outregoptions = " dec(3) bracket excel nocons noobs nor2 nonotes dta"
 global mainoutcomes = "sound words reading readcomp allliter" 

 foreach het in homoclass female{
	 gen T`het'=T_date_2*`het'

	 local j=1
	 foreach Y in $mainoutcomes{
	   //replace option for outreg
	   if `j'==1 local rep = "replace"
	   if `j'>1  local rep = ""
	   
	   areg `Y'_std_ T_date_2 T`het' `het' if date==1, a(allblock) cluster(schoolid)  
	   local pval = r(table)[4,2]
	   areg `Y'_std_ T_date_2 if date==1 & `het'==0, a(allblock) cluster(schoolid)  
	   outreg2 using "$dir/results/T_`het'_0.xml", `rep'  keep(T_date_2) $outregoptions 
	   areg `Y'_std_ T_date_2 if date==1 & `het'==1, a(allblock) cluster(schoolid)  
	   outreg2 using "$dir/results/T_`het'_1.xml", `rep'  keep(T_date_2) addstat(pval, `pval') $outregoptions 

	   
	   local j=`j'+1
	 } 
 }
 
 use using "$dir/results/T_female_0_dta.dta", clear
 append using "$dir/results/T_female_1_dta.dta" 
 append using  "$dir/results/T_homoclass_0_dta.dta"
 append using "$dir/results/T_homoclass_1_dta.dta"

 swaprows, swap(1) and(2)
 drop if _n==1 | _n==7 | _n==8 | _n==9 | _n==14 | _n==15 | _n==20 | _n==21 | _n==22
 
 replace v1="Boys" if _n==3 
 replace v1="Girls" if _n==6
 replace v1="p-value of equal coeffs." if _n==9
 
 replace v1="s.d. below median" if _n==11
 replace v1="s.d. above median" if _n==14
 replace v1="p-value of equal coeffs." if _n==17 
 gen v0=""
 order v0
 replace v0="Gender" in  3
 replace v0="Homogeneity" in 11
 replace v0="of classroom" in 12
 
 label var v2 "Knowledge of letter sounds"
 label var v3 "Reading of non-words"
 label var v4 "Fluency of oral reading"
 label var v5 "Reading comprehension"
 label var v6 "Literacy score" 
 
 #delimit ;
 global notehet2 "Note: Each panel shows estimates of \(\theta_1\) of equation (2) estimated separately for two different groups of control and treated students. The p-values of equal coefficients correspond to the intreaction between treatment and the heterogeneity variable in a separate regression. Homogeneous classroom is an indicator variable that takes value one for classrooms with a s.d of an index of comparable subtasks measured at baseline below the median. All models include cohort, year, and strata fixed effects. The heterogeneity variable is also included as control. Standard errors, shown in squared brackets, are clustered at the school-level (the unit of randomization).  * significant at 10\%; **significant at 5\%; *** significant at 1\%.";
 #delimit cr
 
 texsave using "$dir/results/Appendix_Table_Heterogeneity2.tex", width(\textwidth) ///
 replace size(scriptsize) align(llCCCCC) location(ht) varlabels frag ///
 title(Treatment Effects Heterogeneity by Gender and Classroom Homogeneity) marker(tab:heterogeneity2) ///
 footnote("$notehet2", size(scriptsize)) nofix
 
 foreach het in homoclass female{
     foreach i in 0 1{
	 erase "$dir/results/T_`het'_`i'_dta.dta"
	 erase "$dir/results/T_`het'_`i'.xml"
	 erase "$dir/results/T_`het'_`i'.txt"
 }
 }
  
//APPENDIX TABLE A6: Done in Excel
